package cn.edu.gcu.PraticalTraining.controller;

import java.io.Console;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.gcu.PraticalTraining.domain.Propaganda;
import cn.edu.gcu.PraticalTraining.domain.User;
import cn.edu.gcu.PraticalTraining.service.PropagandaService;
import cn.edu.gcu.PraticalTraining.service.UserService;

/**
 * 宣传管理
 */
@Controller
public class PropagandaManageController {
	private static final Logger log = LoggerFactory.getLogger(PropagandaManageController.class);
///////////////////////////////////////////////////////////////////////////	
	@Autowired
	private PropagandaService propagandaService;
	
	/*@RequestMapping("/propaganda/")
	public ModelAndView index(){
		return new ModelAndView("/propaganda/index");
	}*/
	
	//@ResponseBody
	@RequestMapping("/propaganda/")
	public ModelAndView propaganda(HttpSession httpSession){
		 ArrayList <Propaganda> propaganda = propagandaService.findAll();
		 ModelAndView mav = new ModelAndView("/propaganda/index");
		 
	///////////////////////////////////////////////////////////////////////////	 
		 int n =propaganda.size()/5;
		 if(propaganda.size()%5!=0){
			 n++;
		 }
		 ArrayList <String[]> page =new ArrayList <String[]>();
		 page.clear();
		 String [] Page = new String  [2];
		 Page[0]="/propaganda/page/?Page=1";
		 Page[1]="首页";
		 page.add(Page);
		 Page = new String  [2];
		 Page[0]="/propaganda/page/?Page=1";
		 Page[1]="上一页";
		 page.add(Page);
		 for(int i=0,k=1;i<n;i++,k++){	
			 Page = new String  [2];
			 Page[0]="/propaganda/page/?Page="+k;
			 Page[1]=k+"";
			 page.add(Page);
		 }
		 if(n==1){
			 Page = new String  [2];
			Page[0]="/propaganda/page/?Page=1";
		    Page[1]="下一页";
		    page.add(Page);
		 }else{
			 Page = new String  [2];
			    Page[0]="/propaganda/page/?Page=2";
			    Page[1]="下一页";
			    page.add(Page);		 
		 }
		 Page = new String  [2];
		    Page[0]="/propaganda/page/?Page="+n;
		    Page[1]="尾页";
		    page.add(Page);	
		 
///////////////////////////////////////////////////////////////////////////////
		    
		 ArrayList <Propaganda> list =new ArrayList <Propaganda> (); 
			for(int i=0;i<5&&i<propaganda.size();i++){
				list.add(propaganda.get(i));				
			}
		    mav.addObject("Page",page);
			mav.addObject("PropagandaList",list);
			return mav;
	}
	@RequestMapping("/propaganda/page/")
	public ModelAndView propagandapage(String Page,HttpSession httpSession){	
		 ArrayList <Propaganda> propaganda = propagandaService.findAll();
		 ModelAndView mav = new ModelAndView("/propaganda/index");
		 
		 //////////////////////////////////////
		 int n =propaganda.size()/5;
		 if(propaganda.size()%5!=0){
			 n++;
		 }
		
		 
		 ArrayList <String[]> page =new ArrayList <String[]>();
		 page.clear();
		 String [] Ppage = new String  [2];
		 Ppage[0]="/propaganda/page/?Page=1";
		 Ppage[1]="首页";
		 page.add(Ppage);
		 Ppage = new String  [2];
		 
		 if(Integer.parseInt(Page)==1){
		 Ppage[0]="/propaganda/page/?Page=1";
		 Ppage[1]="上一页";
		 }else{
			 int first=Integer.parseInt(Page)-1;
			 Ppage[0]="/propaganda/page/?Page="+first;
			 Ppage[1]="上一页";
		 }
		 page.add(Ppage);
		 for(int i=0,k=1;i<n;i++,k++){	
			 Ppage = new String  [2];
			 Ppage[0]="/propaganda/page/?Page="+k;
			 Ppage[1]=k+"";
			 page.add(Ppage);
		 }
		 if(Integer.parseInt(Page)==1){
			 Ppage = new String  [2];
			Ppage[0]="/propaganda/page/?Page=2";
		    Ppage[1]="下一页";
		    page.add(Ppage);
		 }else if(Integer.parseInt(Page)==n){
			 Ppage = new String  [2];
		
			    Ppage[0]="/propaganda/page/?Page="+n;
			    Ppage[1]="下一页";
			    page.add(Ppage);
		 }
		 else 
		 {
			 Ppage = new String  [2];
			 int last =Integer.parseInt(Page)+1;
			    Ppage[0]="/propaganda/page/?Page="+last;
			    Ppage[1]="下一页";
			    page.add(Ppage);		 
		 }
		 Ppage = new String  [2];
		    Ppage[0]="/propaganda/page/?Page="+n;
		    Ppage[1]="尾页";
		    page.add(Ppage);	
//////////////////////////////////////////////////////////////////////		 
		  
		    
		 ArrayList <Propaganda> list =new ArrayList <Propaganda> (); 
		 
		 int p = Integer.parseInt(Page)*5;
		 if(Integer.parseInt(Page)==1){
			for(int i=0;i<5&&i<propaganda.size();i++){
				list.add(propaganda.get(i));				
			}
			}else{
				
				for(int i=(Integer.parseInt(Page)-1)*5;i<propaganda.size()&&i<Integer.parseInt(Page)*5;i++){
					list.add(propaganda.get(i));				
				}
			}
		    mav.addObject("Page",page);
			mav.addObject("PropagandaList",list);
			return mav;	
		
		
	}
	
	@RequestMapping("/propaganda/searchliketitleandname/")
	public ModelAndView propagandasearchliketitle(String title,String name,HttpSession httpSession){	
		ArrayList <Propaganda> propaganda = propagandaService.findbytitleandnamelike(title,name);
		 ModelAndView mav = new ModelAndView("/propaganda/index");
		
		   mav.addObject("PropagandaList",propaganda);
			return mav;
	}
	
	////////////////////////////////////////////////////////////////////////
	
	@GetMapping("/propaganda/apply")
	public ModelAndView apply(HttpSession httpSession){
		ModelAndView mav=new ModelAndView("/propaganda/apply");
		User user = (User)httpSession.getAttribute("user");
		mav.addObject("username", user.getUsername());
		return  mav;
	}
	@PostMapping("/propaganda/apply")
	public ModelAndView apply(String title,String category,String content,String remark){
		Propaganda pp=new Propaganda();
		ModelAndView mav = new ModelAndView("/propaganda/apply");
		if(title==""){
			
			mav.addObject("err", "标题不能为空");
			mav.addObject("content", content);
			mav.addObject("remark", remark);
			return mav;
		}
		else if(category=="null"){
			
			mav.addObject("err", "未选类别");
			mav.addObject("title", title);
			mav.addObject("content", content);
			mav.addObject("remark", remark);
			return mav;
		}
		else if(content==""){
			
			mav.addObject("err", "内容不能为空");
			mav.addObject("title", title);
			mav.addObject("remark", remark);
			return mav;
		}
		else{			
			Propaganda newPlan=new Propaganda();
			newPlan.setTitle(title);
			newPlan.setDepartment("血管科");
			newPlan.setCategory(category);
			newPlan.setContent(content);
			newPlan.setRemark(remark);
			
			propagandaService.save(newPlan);
			
			return new ModelAndView("redirect:/propaganda/");
		}
		
	}
}
